package br.com.emmis.tercons;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link IntentionFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link IntentionFragment#newInstance} factory method to
 * create an instance of this fragment.
 *
 */
public class IntentionFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_ID = "ARG_ID";
    private static final String ARG_NAME = "ARG_NAME";
    private static final String ARG_PRECE = "ARG_PRECE";
    private static final String ARG_AMEM = "ARG_AMEM";
    private static final String ARG_POSITION = "ARG_POSITION";


    private int mIntentionId;
    private String mIntentionName;
    private String mIntentionPrece;
    private int mIntentionAmem;
    private int mPosition;

    private EasyTracker mEasyTracker;
    private Button mBtAmen;

    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param name Parameter 1.
     * @param prece Parameter 2.
     * @return A new instance of fragment IntentionFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static IntentionFragment newInstance(int id, String name, String prece, int amem, int position) {
        IntentionFragment fragment = new IntentionFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_ID, id);
        args.putString(ARG_NAME, name);
        args.putString(ARG_PRECE, prece);
        args.putInt(ARG_AMEM, amem);
        args.putInt(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }
    public IntentionFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mEasyTracker = EasyTracker.getInstance(getActivity());
        if (getArguments() != null) {
            mIntentionId = getArguments().getInt(ARG_ID);
            mIntentionName = getArguments().getString(ARG_NAME);
            mIntentionPrece = getArguments().getString(ARG_PRECE);
            mIntentionAmem = getArguments().getInt(ARG_AMEM, 1);
            mPosition = getArguments().getInt(ARG_POSITION, 0);
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        mEasyTracker.activityStop(getActivity());
    }

    @Override
    public void onStart() {
        super.onStart();
        mEasyTracker.activityStart(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_intention, container, false);

        ((TextView)view.findViewById(R.id.tvIntentionName)).setText(mIntentionName);
        ((TextView)view.findViewById(R.id.tvIntentionPrece)).setText(mIntentionPrece);
        final TextView tv = ((TextView)view.findViewById(R.id.tvAmemCount));
        tv.setText(""+mIntentionAmem);
        mBtAmen = (Button)view.findViewById(R.id.btnAmem);


        ((Button)view.findViewById(R.id.btnSendMyIntentions)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
				if(mListener != null) {
					mListener.onMyIntentionButtonClicked();
				}                
            }
        });

        mBtAmen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIntentionAmem++;
                tv.setText(""+mIntentionAmem);
                (view.findViewById(R.id.btnAmem)).setEnabled(false);
				
				if(mListener != null) {
					mListener.onAmemButtonClicked(mIntentionId);
				}
                
            }
        });

        ((Button)view.findViewById(R.id.btnNext)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBtAmen.setEnabled(true);
                mPosition++;
				if(mListener != null) {
					mListener.onNextIntentionButtonClicked(mPosition);
				}
            }
        });

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onMyIntentionButtonClicked();
        public void onAmemButtonClicked(int intentionID);
        public void onNextIntentionButtonClicked(int nextId);
    }

}
